package com.android.sreader;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ChapterListActivity extends ListActivity{
	
	private String[] chapters;
	
	private static class ChapterAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		String[] chapters;
		public ChapterAdapter(Context context,String[] chapters){
			mInflater = LayoutInflater.from(context);
			this.chapters = chapters;
		}
		public int getCount() {
			return chapters.length;
		}

		public String getItem(int i) {
			return chapters[i];
		}

		public long getItemId(int arg0) {
			return arg0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv;
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.chapter_view, null);
				tv = (TextView) convertView.findViewById(R.id.text);
			}else{
				tv = (TextView) convertView.findViewById(R.id.text);
			}
			tv.setText(chapters[position]);
			
			return convertView;
		}
		
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		chapters = this.getIntent().getStringArrayExtra("chapters");
		setListAdapter(new ChapterAdapter(this,chapters));
	}
	
	
	@Override
	protected void onListItemClick (ListView listView, View view, int position, long id){
		super.onListItemClick(listView, view, position, id);
		Intent intent = new Intent();
		intent.putExtra("chapter_num", position);
		this.setResult(RESULT_OK,intent);
		finish();
	}
	
	
}
